<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    width="30%">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="场次名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="场次名称"></el-input>
    </el-form-item>
    <el-form-item label="开始时间" prop="startTime">
      <el-date-picker
      v-model="dataForm.startTime"
      type="datetime"
      value-format="yyyy-MM-dd HH:mm:ss"
      placeholder="选择日期时间">
      </el-date-picker>
    </el-form-item>
    <el-form-item label="结束时间" prop="endTime">
      <el-date-picker
      v-model="dataForm.endTime"
      type="datetime"
      value-format="yyyy-MM-dd HH:mm:ss"
      placeholder="选择日期时间">
      </el-date-picker>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import request from '@/utils/request'
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          name: '',
          startTime: '',
          endTime: ''
        },
        dataRule: {
          name: [
            { required: true, message: '场次名称不能为空', trigger: 'blur' }
          ],
          startTime: [
            { required: true, message: '开始时间不能为空', trigger: 'blur' }
          ],
          endTime: [
            { required: true, message: '结束时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            request.get(`/seckill/session/admin/info/${this.dataForm.id}`).then((data) => {  
              if (data && data.data.code === 200) {
                this.dataForm.name = data.data.session.name
                this.dataForm.startTime = data.data.session.startTime
                this.dataForm.endTime = data.data.session.endTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            request.post(`/seckill/session/admin/${!this.dataForm.id ? 'save' : 'update'}`, {  
              id: this.dataForm.id || undefined,  
              name: this.dataForm.name,  
              startTime: this.dataForm.startTime,  
              endTime: this.dataForm.endTime  
            }).then(response => {  
              if (response.data && response.data.code === 200) {  
                this.$message({  
                  message: '操作成功',  
                  type: 'success',  
                  duration: 1500,  
                  onClose: () => {  
                    this.visible = false  
                    this.$emit('refreshDataList')  
                  }  
                })  
              } else {  
                this.$message.error(response.data.msg)  
              }  
            }).catch(error => {  
              console.error(error)  
            })
          }
        })
      }
    }
  }
</script>
